package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service(version = "1.0.0")//对外提供服务
@Slf4j
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据用户id查询问卷表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Questionnaire> queryQuestionnaire(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, Questionnaire.class);
    }

    /**
     * 根据用户id查询问卷数量
     *
     * @param userId
     * @return
     */
    public Long countQuestionnaire(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.count(query, Questionnaire.class);
    }

    /**
     * 保存问卷
     *
     * @param questionnaire
     * @return
     */
    @Override
    public String saveQuestionnaire(Questionnaire questionnaire) {
        this.mongoTemplate.save(questionnaire);
        return questionnaire.getId().toHexString();
    }

    /**
     * 跟新问卷表
     * @param questionnaire
     * @return
     */
    @Override
    public Questionnaire updateQuestionnaire(Questionnaire questionnaire) {
        Query query = Query.query(Criteria.where("userId").is(questionnaire.getUserId()).and("level").is(questionnaire.getLevel()));
        Update update = Update.update("isLock", questionnaire.getIsLock());
        return this.mongoTemplate.findAndModify(query, update, Questionnaire.class);
    }

    /**
     * 根据level查询试题
     *
     * @param integer
     * @return
     */

    @Override
    public List<Question> queryQuestionByLevel(Integer integer) {
        Query query = Query.query(Criteria.where("level").is(integer));
        return this.mongoTemplate.find(query, Question.class);
    }

    /**
     * 根据试题编号查试题等级
     *
     * @param questionNumber
     * @return
     */
    @Override
    public Question queryQuestionByQuestionNumber(String questionNumber) {
        Query query = Query.query(Criteria.where("questionNumber").is(questionNumber));
        return this.mongoTemplate.findOne(query, Question.class);
    }

    /**
     * 根据试题编号查询选项
     *
     * @param string
     * @return
     */
    @Override
    public List<Option> queryOptionByQuestionNumber(String string) {
        Query query = Query.query(Criteria.where("questionNumber").is(string));
        return this.mongoTemplate.find(query, Option.class);
    }

    /**
     * 根据试题编号和选项编号查询选项表
     *
     * @param questionNumber
     * @param optionNumber
     * @return
     */
    @Override
    public Option queryOptionByQuestionNumAndOptionNum(String questionNumber, String optionNumber) {
        Query query = Query.query(Criteria.where("questionNumber").is(questionNumber).and("optionNumber").is(optionNumber));
        return this.mongoTemplate.findOne(query, Option.class);
    }

    /**
     * 保存结果到报告表
     *
     * @param report
     * @return
     */
    @Override
    public Report saveReport(Report report) {

        //校验
        if (!ObjectUtil.isAllNotEmpty(report.getUserId(), report.getTotalScore(), report.getType())) {
            return null;
        }
        report.setId(ObjectId.get());
        report.setReportId(String.valueOf(System.currentTimeMillis()));
        report.setUpdated(System.currentTimeMillis());
        return this.mongoTemplate.save(report);
        //不要使用 return this.mongoTemplate.save(report).getId().toHexString();
    }

    /**
     * 跟新报告表
     *
     * @param report
     * @return
     */
    @Override
    public Report updateReport(Report report) {
        Query query1 = Query.query(Criteria.where("userId").is(report.getUserId()));
        Update update1 = Update.update("reportId", report.getReportId());
        this.mongoTemplate.findAndModify(query1, update1, Report.class);
        Update update2 = Update.update("totalScore", report.getTotalScore());
        this.mongoTemplate.findAndModify(query1, update2, Report.class);
        Update update3 = Update.update("type", report.getType());
        this.mongoTemplate.findAndModify(query1, update3, Report.class);
        Update update4 = Update.update("updated", report.getUpdated());
        return this.mongoTemplate.findAndModify(query1, update4, Report.class);
    }

    /**
     * 根据用户id查询报告表
     *
     * @param userId
     * @return
     */
    @Override
    public Report queryReportByUserId(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.findOne(query, Report.class);
    }

    /**
     * 根据类型查询测试结果的其他信息
     *
     * @param type
     * @return
     */
    @Override
    public TestResult queryTestResultByType(Integer type) {
        Query query = Query.query(Criteria.where("type").is(type));
        return this.mongoTemplate.findOne(query, TestResult.class);
    }

    /**
     * 根据类型查询测试结果的其他信息
     *
     * @param type
     * @return
     */
    @Override
    public List<Dimension> queryDimensionByType(Integer type) {
        Query query = Query.query(Criteria.where("type").is(type));
        return this.mongoTemplate.find(query, Dimension.class);
    }

    /**
     * 根据报告id查询报告
     *
     * @param reportId
     * @return
     */
    @Override
    public Report queryReportById(String reportId, Long userId) {
        Query query = Query.query(Criteria.where("reportId").is(reportId).and("userId").is(userId));
        return this.mongoTemplate.findOne(query, Report.class);
    }

    /**
     * 根据用户id和类型查询报告表
     *
     * @param userId
     * @param type
     * @return
     */
    @Override
    public List<Report> queryReportByUserIdAndType(Long userId, Integer type) {
        Query query = Query.query(Criteria.where("userId").nin(userId).and("type").is(type)).with(Sort.by(Sort.Order.desc("updated"))).limit(5);
        return this.mongoTemplate.find(query, Report.class);
    }

}
